export function useEditor() {
	let scrollTop = 10000
	
	// 滚动到底部
	async function scrollToBottom() {
		const cpages = getCurrentPages()
		const croute = cpages[cpages.length - 1]?.route
		if(!(croute.includes('pages/chat/single') || croute.includes('pages/chat/group'))){
			return
		}
		
		await uni.pageScrollTo({
			scrollTop,
			duration: 0
		})
		scrollTop += 1000
	}
	
	// 获取底部高度
	async function getFootHeight(placeholderHeight, isBottom=true) {
		await new Promise(resolve => setTimeout(resolve, 200))
		
		// #ifdef H5
		const query = uni.createSelectorQuery()
		const rect = await new Promise((resolve) => {
			query.select('.xbc-editor').boundingClientRect(resolve).exec()
		})
		placeholderHeight = `${rect?.height}px`
		isBottom && await scrollToBottom()
		// #endif
			
		// #ifndef H5
		const query = uni.createSelectorQuery()
		query.selectAll('.xbc-editor').boundingClientRect(res=>{
			console.log(res)
		}).exec()
		// #endif
	}
	
	// 监听键盘高度变化
	function listenKeyboard(res) {
		if(res.height > 0) {
			uni.setStorageSync('uni_keyboard_height', res.height)
			// #ifndef H5
			uni.offKeyboardHeightChange(listenKeyboard)
			// #endif
		}
	}
	
	setTimeout(() => {
		scrollToBottom()
		// #ifndef H5
		uni.onKeyboardHeightChange(listenKeyboard)
		// #endif
	}, 300)
	
	return {
		scrollToBottom,
		getFootHeight,
	}
}